package com.tarena.charging.order.config;

import com.tarena.charging.order.constant.RabbitConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class RabbitmqConfiguration {
    //声明一个交换机 direct类型 接收订单发送的消息
    @Bean
    public DirectExchange delayExchange(){
        return new DirectExchange(RabbitConstant.DELAY_EX);
    }
    //声明一个死信交换机
    @Bean
    public DirectExchange delayDlExchange(){
        return new DirectExchange(RabbitConstant.DELAY_DL_EX);
    }
    //声明一个队列,没有消费者监听 使用延迟路由绑定延迟交换机
    @Bean
    public Queue delayQueue(){
        //帮定正常交换机 要绑定死信交换机
        return QueueBuilder.durable(RabbitConstant.DELAY_QUEUE).deadLetterExchange(RabbitConstant.DELAY_DL_EX)
            .deadLetterRoutingKey(RabbitConstant.DELAY_DL_ROUTING_KEY).ttl(1000*60*3).build();
    }
    //声明一个死信队列
    @Bean
    public Queue delayDlQueue(){
        return QueueBuilder.durable(RabbitConstant.DELAY_DL_QUEUE).build();
    }
    //死信队列 绑定死信交换机 使用死信路由
    @Bean
    public Binding bindDl(){
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with(RabbitConstant.DELAY_ROUTING_KEY);
    }
    //延迟队列绑定延迟交换机 使用延迟路由
    @Bean
    public Binding bind(){
        return BindingBuilder.bind(delayDlQueue()).to(delayDlExchange()).with(RabbitConstant.DELAY_DL_ROUTING_KEY);
    }
}
